<template>
  <div class="container">
    <selectDataOrder
      :per-visible.sync="showOrder"
      @current="selectCurrentRow"
    />
    <per-redio-dialog
      :per-visible.sync="isperShow"
      @getEmployee="getEmployee"
    />
    <div class="search-box">
      <el-form :model="search" label-width="80px">
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="订单单号">
              <el-input v-model="search.saleOrderNo" size="small" placeholder="请输入订单单号"> </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生产单号">
              <el-input v-model="search.produceTaskBillNo" size="small" placeholder="请输入生产单号" >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生产机台">
              <el-select
                        filterable
                v-model="search.equipmentNo"
                size="small"
                clearable
                filterable
                style="width: 100%"
                placeholder="请选择"
                @change="equipmentNoChange"
                @clear="query.equipmentNo = ''"
              >
                <el-option
                  v-for="(item, index) in equipmentNoList"
                  :key="index"
                  :label="item.machineName"
                  :value="item.machineCode"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" style="margin-top: 10px">
          <el-col :span="8">
            <div class="flex">
              <el-form-item label="任务人员"> </el-form-item>
              <el-input
                v-model="name"
                placeholder="请选择人员"
                size="small"
                disabled
              >
                <template slot="append" v-if="name">
                  <el-button
                    slot="append"
                    icon="el-icon-circle-close"
                    @click="employeeClear"
                  />
                </template>
                <template slot="append" v-else>
                  <el-button
                    slot="append"
                    icon="el-icon-user"
                    @click="isperShow = true"
                  />
                </template>
              </el-input>
            </div>
          </el-col>
          <el-col :span="8">
            <el-form-item label="日期范围">
              <el-date-picker
                v-model="valueDate"
                style="width: 100%"
                size="small"
                type="daterange"
                range-separator="-"
                value-format="yyyy-MM-dd"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                @change="handleDateChange"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="margin-top: 10px">
          <el-col>
            <el-button
              type="primary"
              size="small"
              icon="el-icon-search"
              style="margin-left: 80px"
              :loading="searchLoading"
              @click="handleSearch"
              >搜索</el-button
            >
            <el-button
              size="small"
              icon="el-icon-download"
              :loading="downLoading"
              @click="downloadFile"
              >导出数据</el-button
            >
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="d_main flex1">
      <!-- 操作区 S-->
      <!-- 操作区 E-->
      <!-- 表格操作区 S-->
      <div class="d_tableOperation">
        <div class="d_condition"></div>
        <div class="d_set">
          <div @click="Refresh"><i class="d_icon el-icon-refresh-right" /></div>
          <div class="setting flex">
            <HeadSeting
              :list-data.sync="tableHead"
              :table-code="$config.produceworktaskreportlist"
              @reset="getTableHead"
            />
          </div>
        </div>
      </div>
      <!-- 表格操作区 E-->
      <!--table板块-->
      <div ref="tableCon" class="tableCon expandBox">
        <el-table
          v-show="tableHead.length > 0"
          id="emtable"
          ref="multipleTable"
          v-loading="searchLoading"
          :data="allMaterirls"
          :height="tableHeight || 'auto'"
          border
          fit
          style="width: 100%"
          @header-dragend="surverWidth"
        >
          <template v-for="(fruit, index) in tableHead">
            <el-table-column
              v-if="fruit.visible"
              :key="index"
              :label="fruit.columnName"
              :min-width="fruit.columnWidth || 90"
              :prop="fruit.columnCode"
              show-overflow-tooltip
              :fixed="fruit.isFloat ? 'left' : false"
            >
              <template slot-scope="scope">
                <el-button
                  v-if="fruit.columnCode === 'getMaterialBillNo'"
                  type="text"
                  @click="checkProductList(scope.row)"
                >
                  {{ scope.row[fruit.columnCode] }}</el-button
                >
                <div
                  v-else-if="fruit.columnCode === 'getMaterialBillStateName'"
                  class="flex"
                >
                  <template v-if="scope.row.getMaterialBillState === 0">
                    <span class="mr5 spot" style="background: #3f61f1" />{{
                      scope.row[fruit.columnCode]
                    }}
                  </template>
                  <template
                    v-else-if="
                      scope.row.getMaterialBillState === 1 ||
                      scope.row.getMaterialBillState === 3
                    "
                  >
                    <span class="mr5 spot" style="background: #67c23a" />{{
                      scope.row[fruit.columnCode]
                    }}
                  </template>
                  <template v-else>
                    <span class="mr5 spot" style="background: #999aaa" />{{
                      scope.row[fruit.columnCode]
                    }}
                  </template>
                </div>
                <div
                  v-else-if="fruit.columnCode === 'outStockStateName'"
                  class="flex"
                >
                  <template v-if="scope.row.outStockState === 0">
                    <span class="mr5 spot" style="background: #3f61f1" />{{
                      scope.row[fruit.columnCode]
                    }}
                  </template>
                  <template v-else-if="scope.row.outStockState === 1">
                    <span class="mr5 spot" style="background: #67c23a" />{{
                      scope.row[fruit.columnCode]
                    }}
                  </template>
                  <template v-else>
                    <span class="mr5 spot" style="background: #e13335" />{{
                      scope.row[fruit.columnCode]
                    }}
                  </template>
                </div>
                <template v-else>
                  {{ scope.row[fruit.columnCode] }}
                </template>
              </template></el-table-column
            >
          </template>
        </el-table>
        <!-- 骨架屏     -->
        <div v-if="tableHead.length == 0" class="p16">
          <el-skeleton :rows="6" animated />
        </div>
      </div>
      <!-- 分页 S-->
      <div class="d_pag">
        <pagination
          :total="total || 0"
          :page.sync="search.pageIndex"
          :limit.sync="search.pageSize"
          @pagination="getAllMaterils"
        />
      </div>
    </div>
  </div>
</template>
<script>
import { arraySort } from "@/utils";
import HeadSeting from "@/components/HeadSeting";
import Pagination from "@/components/Pagination";
import { mapGetters } from "vuex";
import { fileExport } from "@/utils/index";
import selectDataOrder from "@/components/selectDataOrder";
import perRedioDialog from "@/components/perDialogRedio";
export default {
  name: "workingHours",
  components: {
    Pagination,
    HeadSeting,
    selectDataOrder,
    perRedioDialog,
  },
  data() {
    return {
      equipmentNoList: [], //生产机台下拉数据
      searchLoading: false, //表格加载层
      downLoading: false, //下载文件按钮加载层
      showOrder: false,
      isperShow: false,
      valueDate: "",
      allMaterirls: [],
      tableHeight: null,
      name:'',
      // 搜索条件
      search: {
        pageIndex: 1,
        pageSize: 30,
        likeWords: "",
        saleOrderNo: "", //销售订单号，空值查询全部
        produceTaskBillNo: "", //生产任务单号，空值查询全部
        equipmentNo: "", //设备代码（机台代码），空值查询全部
        workPerson: "", //作业人员代码，空值查询全部
        beginTime: "",
        endTime: "",
      },
      total: 0, // 总条数
      tableHead: [], // 表格表头
    };
  },
  computed: {
    ...mapGetters(["userInfo"]),
  },
  created() {
    //  所有列表数据
    this.getAllMaterils();
    // 表格得表头
    this.getTableHead();

    //获取机台下拉数据
    this.getEquipmentNoList();
  },
  mounted() {
    // 表格高度
    this.calHeight();
    console.log(this.ProductForm);
  },
  updated() {
    this.$nextTick(() => {
      this.$refs.multipleTable.doLayout();
    });
  },
  methods: {
    //获取物料下拉数据
    getmaterialList() {
      let parameter = {
        groupCode: "",
        categoryCode: "",
        likeWords: "",
      };
      this.$api.report.GetGoodsDataList(parameter).then((res) => {
        this.materialList = res;
      });
    },

    //点击搜索
    handleSearch(){
      this.search.pageIndex = 1
      this.getAllMaterils();
    },

    //选择订单事件
    selectCurrentRow(row) {
      this.search.saleOrderNo = row.saleOrderNo;
      this.query.saleOrderNo = row.saleOrderNo;
      this.getAllMaterils();
    },

    //物料下拉选择事件
    handleMaterialChange(e) {
      this.query.goodsCode = e;
      this.getEquipmentNoList();
    },

    //获取生产机台下拉列表数据
    getEquipmentNoList() {
      this.$api.report.GetMatchProductDevices({ likeWords: "" }).then((res) => {
        this.equipmentNoList = res;
      });
    },

    //规格型号下拉事件
    equipmentNoChange(e) {
      this.query.stockGoodsCode = e;
    },

    //日期范围选择事件
    handleDateChange() {
      if (this.valueDate) {
        this.search.beginTime = this.valueDate[0];
        this.search.endTime = this.valueDate[1];
      } else {
        this.search.beginTime = "";
        this.search.endTime = "";
      }
    },

    // 得到申请人员
    getEmployee(val) {
        this.search.workPerson = val.employeeCode
        this.name = val.employeeName
    },

    //任务人员失去焦点事件
    employeeClear(){
            this.search.workPerson = ''
            this.name = ''
    },

    //导出数据
    downloadFile() {
      this.downLoading = true;
      this.$api.report
        .ExportProduceCraftWorkTaskReportsToExcel(this.search)
        .then((res) => {
          fileExport(res, "工时导出数据", "xlsx");
          this.downLoading = false;
        });
    },

    // 保存表头样式变化
    saveHead(newArray) {
      const parmas = newArray;
      setTimeout(() => {
        this.$api.common.saveTableStyle(parmas).then(() => {});
      }, 300);
    },
    // 保存表头列宽度
    surverWidth(newWidth, oldWidth, column) {
      setTimeout(() => {
        const heads = [...this.tableHead];
        heads.forEach((item) => {
          if (item.columnCode === column.property) {
            item.columnWidth = column.width;
            return;
          }
        });
        this.saveHead(heads);
      }, 100);
    },
    // 表格最大高度
    calHeight() {
      this.$nextTick(() => {
        const rect = this.$refs.tableCon.getBoundingClientRect();
        console.log(rect.height);
        this.tableHeight = rect.height;
      });
    },
    // ，清除所有查询条件
    Refresh() {
      this.search = this.$options.data().search;
      this.getAllMaterils();
    },
    // 获取表格表头
    getTableHead() {
      const parmas = {
        tableCode: this.$config.produceworktaskreportlist,
      };
      this.$api.common.getAllTableHead(parmas).then((res) => {
        res.sort(arraySort("columnSort"));
        this.tableHead = res;
      });
    },
    // 所有列表数据
    getAllMaterils() {
      const parmas = this.search;
      this.searchLoading = true;
      this.$api.report
        .PageQueryProduceCraftWorkTaskReportsData(parmas)
        .then((res) => {
          this.allMaterirls = res.list;
          this.allMaterirls.forEach((item)=>{
            for(let i in item){
              if(!item[i] && item[i] !== 0){
                item[i] = '--'
              }
            }
          })
          this.total = res.totalCount;
          this.searchLoading = false;
        })
        .catch(() => {
          this.searchLoading = false;
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.search-box {
  padding: 10px 16px 0 0;
  margin-bottom: 10px;
  height: 160px;
  background: #ffffff;
  border-radius: 4px;
  border: 1px solid #e6e6e6;
  .el-form-item {
    margin-bottom: 0px !important;
  }
}
.container {
  display: flex;
  flex-direction: column;
  height: 100%;
}
.permangeRg {
  border: 1px solid #e6e6e6;
  background: #fff;
  height: 100%;

  .mainTT {
    font-size: 16px;
    font-weight: bold;
    width: 100%;
    padding: 18px 16px;
    padding-bottom: 0;
  }
}
.addWuliaoBox {
  padding: 10px 16px;

  .tableBox {
    margin-top: 16px;
    border: 1px solid #e6e6e6;
    border-bottom: none;
    border-radius: 8px 8px 0 0;
    overflow: hidden;

    .tt {
      height: 38px;
      line-height: 38px;
      padding-left: 16px;
      background: #f7f8fa;
      border-radius: 8px 8px 0 0;
    }
  }
}

.subTip {
  border-bottom: 1px #e6e6e6 solid;
  background: #f5f6f9;
  padding: 8px 16px;
}
</style>
